from myelin.Ns import MYELINS
from myelin.rdf.Ns import RDFS_SEEALSO
from myelin.rdf.bindery.Model import OBJECT_TYPE_RESOURCE
from nodetypes.ntypes.Container.showContains.RDF import Transform, Document


class Document(Document):
    
    def getRelatedStatements(self, context, consumer, driver, locale):
        return self.getNodeGroupingStatements(
            context,
            super(Document,self).getRelatedStatements(
                context, consumer, driver, locale
            ),
            driver,
            locale
        )
    
    def getNodeGroupingStatements(self, context, consumer, driver, locale):
        root = context.defaults.document_root
        
        action = context.action
        acls = context.acls.checkNodeAcls
        model = context.model
        
        mid = model.rdf_value
        #gas = self.getAgentStatements
        href = context.urlFormatter.newUrlMapper(True)
        
        # order nodes into their groupings
        for pp,p in model.rdf_toRelations('mParent'):
            p = p.rdf_clone(driver)
            phit = 0
            pid = p.rdf_value
            for np,n in p.rdf_toRelations('mContains'):
                nhit = 0
                nid = n.rdf_value
                for gp,g in n.rdf_toRelations('mGrouping'):
                    if g.rdf_value == mid and \
                        acls(n.rdf_domain, action):
                        nhit = 1
                        # id(p) - mGrouping -> id(n)
                        consumer((
                            g.rdf_asStatement(
                                gp,
                                nid
                            ),
                        ))                        
                       
                        nrs = n.rdf_toStatements
                        consumer(nrs('rdfType'))
                        consumer(nrs('mTag'))
                        consumer(nrs('dcTitle'))
                        #consumer(nrs('dcDescription'))
                        consumer(nrs('dcLanguage'))
                        
                        #consumer(nrs('dctermsCreated'))
                        #consumer(nrs('dcCreator'))
                        #for m in n.dcCreator:
                        #    gas(context, consumer, m, locale)
                        #
                        #consumer(nrs('dctermsModified'))
                        #consumer(nrs('mLast_Modifier'))
                        #for m in n.mLast_Modifier:
                        #    gas(context, consumer, m, locale)
                            
                if nhit:
                    consumer((
                        n.rdf_asStatement(
                            np,
                            pid
                        ),
                    ))
                    nrs = n.rdf_toStatements
                        
                    consumer(nrs('rdfType')) 
                    consumer(nrs('dcTitle'))
                    consumer(nrs('dcLanguage'))
                    
                    o = href(n.rdf_domain)
                    if o:
                        consumer((
                            (
                                n.rdf_value,
                                RDFS_SEEALSO,
                                o,
                                '',
                                n.rdf_domain,
                                OBJECT_TYPE_RESOURCE,
                            ),
                        ))
                    
                    phit = 1
                    
            if phit:
                consumer((
                    p.rdf_asStatement(
                        pp,
                        mid
                    ),
                ))
                
                nrs = p.rdf_toStatements
                        
                consumer(nrs('rdfType')) 
                consumer(nrs('dcTitle'))
                consumer(nrs('dcLanguage'))
                
                o = href(p.rdf_domain)
                if o:
                    consumer((
                        (
                            p.rdf_value,
                            RDFS_SEEALSO,
                            o,
                            '',
                            p.rdf_domain,
                            OBJECT_TYPE_RESOURCE,
                        ),
                    ))
                                
        for tp,t in model.rdf_toRelations('mTranslation_Of'):
            thit = 0
            tid = t.rdf_value
            for pp,p in t.rdf_toRelations('mParent'):
                phit = 0
                pid = p.rdf_value
                for cp,c in p.rdf_toRelations('mContains'):                    
                    chit = 0
                    cid = c.rdf_value
                    for gp,g in c.rdf_toRelations('mGrouping'):
                        if tid == g.rdf_value:                            
                            ghit = 0
                            for np,n in c.rdf_toRelations('mTranslation'):
                                if locale in n.dcLanguage and \
                                    acls(n.rdf_domain, action):
                                    ghit = 1
                                    # id(p) - mGrouping -> id(n)
                                    consumer((
                                        n.rdf_asStatement(
                                            np,
                                            cid
                                        ),
                                    ))
                                            
                                    nrs = n.rdf_toStatements
                                    consumer(nrs('rdfType'))
                                    consumer(nrs('mTag'))
                                    consumer(nrs('dcTitle'))
                                    #consumer(nrs('dcDescription'))
                                    consumer(nrs('dcLanguage'))
                                    
                                    #consumer(nrs('dctermsCreated'))
                                    #consumer(nrs('dcCreator'))
                                    #for m in n.dcCreator:
                                    #    gas(context, consumer, m, locale)
                                    #
                                    #consumer(nrs('dctermsModified'))
                                    #consumer(nrs('mLast_Modifier'))
                                    #for m in n.mLast_Modifier:
                                    #    gas(context, consumer, m, locale)
                            
                            if ghit:
                                consumer((
                                    g.rdf_asStatement(
                                        gp,
                                        cid
                                    ),
                                ))
                                
                                nrs = g.rdf_toStatements
                        
                                consumer(nrs('rdfType')) 
                                consumer(nrs('dcTitle'))
                                consumer(nrs('dcLanguage'))
                                
                                o = href(n.rdf_domain)
                                if o:
                                    consumer((
                                        (
                                            g.rdf_value,
                                            RDFS_SEEALSO,
                                            o,
                                            '',
                                            g.rdf_domain,
                                            OBJECT_TYPE_RESOURCE,
                                        ),
                                    ))
                                
                                chit = 1
                    if chit:
                        consumer((
                            c.rdf_asStatement(
                                cp,
                                pid
                            ),
                        ))
                        
                        nrs = c.rdf_toStatements
                        
                        consumer(nrs('rdfType')) 
                        consumer(nrs('dcTitle'))
                        consumer(nrs('dcLanguage'))
                         
                        o = href(c.rdf_domain)
                        if o:
                            consumer((
                                (
                                    c.rdf_value,
                                    RDFS_SEEALSO,
                                    o,
                                    '',
                                    c.rdf_domain,
                                    OBJECT_TYPE_RESOURCE,
                                ),
                            ))
                        phit = 1
                        
                if phit:
                    consumer((
                        p.rdf_asStatement(
                            pp,
                            tid
                        ),
                    ))
                    
                    nrs = p.rdf_toStatements
                        
                    consumer(nrs('rdfType')) 
                    consumer(nrs('dcTitle'))
                    consumer(nrs('dcLanguage'))
                    
                    o = href(p.rdf_domain)
                    if o:
                        consumer((
                            (
                                p.rdf_value,
                                RDFS_SEEALSO,
                                o,
                                '',
                                p.rdf_domain,
                                OBJECT_TYPE_RESOURCE,
                            ),
                        ))
                    
                    thit = 1
                    
            if thit:
                consumer((
                    t.rdf_asStatement(
                        tp,
                        mid
                    ),
                ))
                
                nrs = t.rdf_toStatements
                        
                consumer(nrs('rdfType')) 
                consumer(nrs('dcTitle'))
                consumer(nrs('dcLanguage'))
                
                o = href(t.rdf_domain)
                if o:
                    consumer((
                        (
                            t.rdf_value,
                            RDFS_SEEALSO,
                            o,
                            '',
                            t.rdf_domain,
                            OBJECT_TYPE_RESOURCE,
                        ),
                    ))
                
        return consumer
    
    
class Transform(Transform):
    
    newDocument = Document